﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using SimpleBlog.Infrastructure.Repositories;
using SimpleBlog.Models;
using SimpleBlog.Mvc;

namespace SimpleBlog.Areas.Admin.Controllers
{
    public class ConfigurationController : Controller
    {
        IConfigurationRepository _configurationRepository;

        public ConfigurationController(IConfigurationRepository configurationRepository)
        {
            _configurationRepository = configurationRepository;
        }

        [HttpGet]
        [Authorize]
        public ActionResult Index()
        {
            var configFields = _configurationRepository.GetAll();

            return View(configFields);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Save()
        {
            FormCollection collection = new FormCollection(Request.Unvalidated().Form);
            foreach (var key in collection.AllKeys)
            {
                _configurationRepository[key] = collection[key];
            }
            return RedirectToAction("Index");
        }

    }
}
